package panel.viewSpots.navigationNew
{
	import assists.resource.ResourceLoader;
	
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.TransformGestureEvent;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	public class PicSlideGroup extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		
		private var _picList:Array = [];
		private var _picContainer:Sprite = new Sprite();
		private var _index:int = 0;
		
		private var _cicirleGroup:Sprite = new Sprite();
		private var _circle:Shape = new Shape();
		private var _lock:Boolean;
		
		private static const CIRCLE_RADIUS:int = 10;
		private static const CIRCLE_DIS:int = 40;

		private var _circleBack:Shape;
		public function PicSlideGroup()
		{
			super();
		}
		
		public function init(stage:Stage,loader:ResourceLoader):void
		{
			_stage = stage;
			_loader = loader;
		}
		
		protected function clickHandler(event:TransformGestureEvent):void
		{
			if(event.offsetX == 1)
			{
				move(1);	
			}else
			{
				move(-1);
			}
		}
		
		private function move(dir:int):void
		{
			if(_lock) return;
			if(dir == -1)
			{
				if(_index == _picList.length - 1) return;
				++ _index;
			}
			else
			{
				if(_index == 0) return;
				-- _index;
			}
			movePic(dir);
			moveCircle();
		}
		
		
		public function show(picList:Array):void
		{
			if(picList.length == 0)
			{
				var bmp:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.noPic,true);
				bmp.width = _stage.stageWidth;
				bmp.height = _stage.stageHeight * 0.3;
				picList.push(bmp);
				_picContainer.addChild(picList[0]);
			}else
			{
				_picList = picList;
				Multitouch.inputMode = MultitouchInputMode.GESTURE;
				_picContainer.addEventListener(TransformGestureEvent.GESTURE_SWIPE,clickHandler);
				_picContainer.addChild(picList[0]);
				layout();
			}
			
		}
		
		private function layout():void
		{
			drawCircle();
			
			_cicirleGroup.x = (_picContainer.width - _cicirleGroup.width) * 0.5;
			_cicirleGroup.y = _picContainer.height - _cicirleGroup.height;
			_circleBack = new Shape();
			_circleBack.graphics.beginFill(0,0.5);
			_circleBack.graphics.drawRoundRect(-CIRCLE_RADIUS * 2 ,- _cicirleGroup.height,_cicirleGroup.width + CIRCLE_RADIUS * 2,_cicirleGroup.height + _cicirleGroup.height,_cicirleGroup.height *0.5,_cicirleGroup.height *0.5);
			_circleBack.graphics.endFill();
			_cicirleGroup.addChildAt(_circleBack,0);
			
			_cicirleGroup.addChild(_circle);
			this.addChild(_picContainer);
			this.addChild(_cicirleGroup);
			
		}
		/**
		 * 
		 * @param dir 1 为向右划 -1为向作画
		 * 
		 */		
		private function movePic(dir:int):void
		{
			_lock = true;
			var bmp:Bitmap =_picList[_index + dir];
			_picList[_index].x = - dir * _picList[_index].width
			_picContainer.addChild(_picList[_index]);
			TweenLite.to(_picList[_index],1,{x:_picList[_index].x + dir * _picList[_index].width});
			TweenLite.to(bmp,1,{x:bmp.x + dir * bmp.width,onComplete:movePicEnd,onCompleteParams:[dir]});
		}
		
		private function movePicEnd(dir:*):void
		{
			_lock = false;
			_picContainer.removeChild(_picList[_index + dir]);
		}
		
		private function moveCircle():void
		{
			TweenLite.to(_circle,1,{x:_index * CIRCLE_DIS});
		}
		
		private function drawCircle():void
		{
			_cicirleGroup.graphics.clear();
			_circle.graphics.beginFill(0xffffff);
			_circle.graphics.drawCircle(0,0,CIRCLE_RADIUS);
			_circle.graphics.endFill();
			
			for(var i:int = 0;i<_picList.length;i++)
			{
				drawCircleBase(i * CIRCLE_DIS);
			}
		}
		
		private function drawCircleBase(poxX:int):void
		{
			
			_cicirleGroup.graphics.beginFill(0xffffff,0.9);
			_cicirleGroup.graphics.drawCircle(poxX,0,CIRCLE_RADIUS);
			_cicirleGroup.graphics.endFill();
		}
		
		public function clear():void
		{
			if(_circleBack)
			{
			_circleBack.graphics.clear();
			_cicirleGroup.graphics.clear();
			}
			_picContainer.removeEventListener(TransformGestureEvent.GESTURE_SWIPE,clickHandler);
		}
		
	}
}